//
//  DetailOrderVC.m
//  dianfan
//
//  Created by Ye Jin on 15/11/11.
//  Copyright © 2015年 Ye Jin. All rights reserved.
//

#import "DetailOrderVC.h"
#import <BmobSDK/bmob.h>
#import "MBProgressHUD.h"
#import "logoutVC.h"

@interface DetailOrderVC ()

@property (nonatomic) NSMutableArray *userList;

@end

@implementation DetailOrderVC

- (void)logout
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    logoutVC *lvc = [sb instantiateViewControllerWithIdentifier:@"logout"];
    [self.navigationController pushViewController:lvc animated:YES];
}

- (void)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = self.order.dishName;
    self.userList = [[NSMutableArray alloc] init];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(back:)];
    
    BmobUser *bUser = [BmobUser getCurrentUser];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:bUser.username
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(logout)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Order"];
    NSDictionary *condiction1 = @{@"createdAt":@{@"$gte":@{@"__type": @"Date", @"iso": [NSString stringWithFormat:@"%@ 00:00:00",self.startString]}}};
    NSDictionary *condiction2 = @{@"createdAt":@{@"$lt":@{@"__type": @"Date", @"iso": [NSString stringWithFormat:@"%@ 00:00:00",self.endString]}}};
    NSArray *condictonArray = @[condiction1,condiction2];
    [bquery addTheConstraintByAndOperationWithArray:condictonArray];
    
    BmobQuery *inQuery = [BmobQuery queryWithClassName:@"Menu"];
    [inQuery whereKey:@"dishName" equalTo:self.order.dishName];
    [bquery whereKey:@"dish" matchesQuery:inQuery];
     [bquery includeKey:@"user"];
    
    //转菊花
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    hud.labelText=@"loading";
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            [self.userList addObject:@[[obj objectForKey:@"createdAt"],[[obj objectForKey:@"user"] objectForKey:@"username"],[obj objectForKey:@"dishNum"]]];
        }
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.userList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"detailCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@: %d",[[self.userList objectAtIndex:indexPath.row][0] substringFromIndex:11],[self.userList objectAtIndex:indexPath.row][1],[[self.userList objectAtIndex:indexPath.row][2] intValue]];
    
    
    return cell;
}

@end
